<?php
	session_start();
	require_once('common/config.php');
	require_once('../_helper/helper.php');
	require_once('../_model/album_model.php');
	


	if(!isset($_SESSION['logon']))
	{
		header("Location: /wks/login.php?url=product");
		exit();
	}
	if(isset($_GET['album_id']))
		$album_id = $_GET['album_id'];
	else
		$album_id = null;
		
	$action = $_GET['action'];
	$pageCMS = 999;
	
	if($action == 'list' )
	{
		//$supplier_arr =	GetSupplierAll();
		$album_value= GetAlbumById($album_id);
		$photo_arr = GetPhotoByAlbumId($album_id);
		
				
		//Xu ly
		require_once('template/photo/photo_list.php');
		exit();
	}
	if($action == 'del')
	{
		
		$result = FALSE;
		if (isset($_GET['photo_id']))
			$result = DeletePhoto($_GET['photo_id']);
			
		if($result == FALSE)
			header("Location: /wks/photo.php?action=list&album_id=$album_id&error=1&e_action=del");	
		else
		{
			header("Location: /wks/photo.php?action=list&album_id=$album_id");	
		}
		exit();
	}
	if($action == 'add')
	{
		if($_GET['album_id'] != '' && $_GET['photo_url'])
		{
			$result = InsertPhoto($_GET);
			
			if($result)
			{
				$arr = GetPhotoByPhotoId(mysql_insert_id());
				
				echo json_encode($arr);
				
			}
			else
			{
				$result = array('result'=>'error');
				echo json_encode($result);
			}
		}
		else
			header("Location: /wks/photo.php?action=list&album_id=$album_id&error=1&e_action=add");
		exit();
	}
	if($action == 'adjorder')
	{
		//var_dump($_GET); die();
		$photo_id = $_GET['id'];
		
		if($_GET['adj'] == 'inc')
		{
			if (UpdatePhotoOrder($photo_id, '+') )
				echo "ok";
			else
				echo "fail";
		}
		if($_GET['adj'] == 'desc')
		{
			if (UpdatePhotoOrder($photo_id, '-') )
				echo "ok";
			else
				echo "fail";
		}
		exit();
	}

	
	
?>